package com.blockmeta.bbs.baselibrary.widget;

import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.HorizontalScrollView;

/**
 * @author AirThor
 * @date 2018/6/21.
 */

public class NestedHorizontalScrollView extends HorizontalScrollView {
    private static final long DELAY = 200;

    private int currentScroll;

    private boolean isScrollByDrag = false;

    private int scrollX=0;

    private float oldX = -1;

    private Runnable scrollCheckTask=new Runnable() {
        @Override
        public void run() {
            int newScroll = getScrollX();
            if (currentScroll == newScroll) {
                if (onScrollListener != null) {
                    isScrollByDrag = false;
                    onScrollListener.onScrollStopped();
                }
            } else {
                currentScroll = getScrollX();
                postDelayed(scrollCheckTask, DELAY);
            }
        }
    };
    private float xDistance,yDistance,lastX,lastY;

    public NestedHorizontalScrollView(Context context) {
        super(context);
    }

    public NestedHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NestedHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        scrollX = l;
        if (onScrollListener != null&&isScrollByDrag) {
            onScrollListener.onScrollChanged(l, t, oldl, oldt);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                lastX = ev.getX();
                lastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance += Math.abs(curX - lastX);
                yDistance += Math.abs(curY - lastY);
                lastX = curX;
                lastY = curY;
                if (xDistance*0.5 > yDistance) {
                        return true;
                    }
                break;
            case MotionEvent.ACTION_UP:
                yDistance=xDistance=0f;
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_UP:
                oldX = -1;
                currentScroll = getScrollX();
                postDelayed(scrollCheckTask,DELAY);
                break;
            case MotionEvent.ACTION_MOVE:
                isScrollByDrag = true;
                if (oldX == -1) {oldX = ev.getX();}
                float currentX = ev.getX();
                if (currentX-oldX>0&&scrollX==0){
                    ViewParent viewParent = getParent();
                    while (viewParent!= null&&!(viewParent instanceof ViewPager)){
                        viewParent = viewParent.getParent();
                    }
                    if (viewParent!=null) {
                        viewParent.requestDisallowInterceptTouchEvent(false);
                    }
                }
                break;
            default:
                break;
        }
        return super.onTouchEvent(ev);
    }

    public interface OnScrollListener {
        /**
         *  同onScrollChanged
         * @param x
         * @param y
         * @param oldX
         * @param oldY
         */
        void onScrollChanged(int x, int y, int oldX, int oldY);

        /**
         * 滚动停止
         */
        void onScrollStopped();
    }

    /**
     * 暴露onScrollChanged方法
     */
    private OnScrollListener onScrollListener;

    /**
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }
}
